﻿using System;

using TempEmployees.BLL.Database;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class WebForm3 : ABasePage
    {
        private int RadioButtonListSearch
        {
            set
            {
                if (value == 0) RadioByID.Checked = true;
                else if (value == 1) RadioByFilters.Checked = true;
            }
            get
            {
                if (RadioByID.Checked) return 0;
                else return 1;
            }
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            //page defualt button
            Page.Form.DefaultButton = ButtonSearchEmployee.UniqueID;
            //gridview delegate
            GridViewEmployees1.GotoEmployeeDetailes = GotoEmployeeDetailes;
            GridViewEmployees1.GotoProduceForms = GotoProduceForms;

            if (!this.IsPostBack)
            {
                SetDropDownLists();   
                if (!APageData.Exist<SearchEmployeesStoreData>())
                {
                    InitDropDownLists();
                }
                else
                {
                    RetrieveData();
                }
                RadioButtonListSearch = 1;
                SetGridView();
            }
        }

        private void SetGridView()
        {
            if (RadioButtonListSearch == 0) //search by employee ID
            {
                if (EEmployee.GetEmployeeByID(TextBoxSearchId.Text) != null)
                {
                    GotoEmployeeDetailes(TextBoxSearchId.Text);
                }
                return;
            }

            Filters filters = new Filters();
           
            if (DropDownListUsers.SelectedIndex != 0)
            {
                filters.Add("UserID", DropDownListUsers.SelectedValue);
            }
            if (DropDownListBudgetTypes.SelectedIndex != 0)
            {
                filters.Add("Budget.TypeID", int.Parse(DropDownListBudgetTypes.SelectedValue));
            }
            if (DropDownListDepartment.SelectedIndex != 0)
            {
                filters.Add("DepartmentID", int.Parse(DropDownListDepartment.SelectedValue));
            }
            if (DropDownListSemester.SelectedIndex != 0)
            {
                filters.Add("SemesterID", int.Parse(DropDownListSemester.SelectedValue));
            }

            GridViewEmployees1.SetGridView(filters);
        }

        private void InitDropDownLists()
        {
            bool show;
            switch (State.UserRole)
            {
                case DbConstants.Roles.Employer:
                    show = false;
                    DropDownListUsers.SelectedValue = State.UserId.ToString();
                    break;
                default:
                    show = true;
                    break;
            }
            DropDownListUsers.Visible = show;
            LabelUser.Visible = show;

            DropDownListBudgetTypes.SelectedValue = ((int)DbConstants.BudgetType.Technioni).ToString();
        }

        private void SetDropDownLists()
        {
            DbServices.SetDropDownBudgetTypes(DropDownListBudgetTypes);

            DbServices.SetDropDownUsers(DropDownListUsers);

            if (State.UserRole == DbConstants.Roles.Employer)
            {
                DropDownListDepartment.Visible = false;
                LabelDropDownDep.Visible = false;
            }
            DbServices.SetDropDownDepLab(DropDownListDepartment);

            DbServices.SetDropDownSemesters(DropDownListSemester, true);
        }

        protected void ButtonSearchEmployee_Click(object sender, EventArgs e)
        {
            SetGridView();
        }

        private void GotoEmployeeDetailes(string employeeId)
        {
            StoreData();

            APageData.Set(new EmployeeDetailsPageData(employeeId, GUIServices.PAGE_SEARCH_EMPLOYEES));

            Response.Redirect(GUIServices.PAGE_UPDATE_EMPLOYEE);
        }

        private void GotoProduceForms(string employeeId)
        {
            StoreData();

            APageData.Set(new ProduceEmployeeFormsData(employeeId, GUIServices.PAGE_SEARCH_EMPLOYEES));

            Response.Redirect(GUIServices.PAGE_PRODUCE_FORMS);
        }

        private void StoreData()
        {
            APageData.Set(new SearchEmployeesStoreData(DropDownListBudgetTypes.SelectedIndex,
                DropDownListDepartment.SelectedIndex, DropDownListSemester.SelectedIndex, DropDownListUsers.SelectedIndex,
                TextBoxSearchId.Text, DropDownListUsers.Visible));
        }

        private void RetrieveData()
        {
            SearchEmployeesStoreData data = APageData.Get<SearchEmployeesStoreData>();

            DropDownListBudgetTypes.SelectedIndex = data.DropDownBudgetTypeIndex;
            DropDownListDepartment.SelectedIndex = data.DropDownDepIndex;
            DropDownListSemester.SelectedIndex = data.DropDownSemesterIndex;
            DropDownListUsers.SelectedIndex = data.DropDownUsersIndex;
            TextBoxSearchId.Text = data.TextBoxSearchId;
            LabelUser.Visible = data.ShowDropDownUsers;
            DropDownListUsers.Visible = data.ShowDropDownUsers;

            data.Remove();
        }

        protected void ExportToExcel_Click(object sender, EventArgs e) { GridViewEmployees1.ExportToExcel(); }
    }

    internal class SearchEmployeesStoreData : APageData
    {
        private int m_dropDownBudgetTypeIndex;
        private int m_dropDownDepIndex;
        private int m_dropDownSemesterIndex;
        private int m_dropDownUsersIndex;
        private string m_textBoxSearchId;
        private bool m_showDropDownUsers;

        public int DropDownBudgetTypeIndex { get { return m_dropDownBudgetTypeIndex; } }
        public int DropDownDepIndex { get { return m_dropDownDepIndex; } }
        public int DropDownSemesterIndex { get { return m_dropDownSemesterIndex; } }
        public int DropDownUsersIndex { get { return m_dropDownUsersIndex; } }
        public string TextBoxSearchId { get { return m_textBoxSearchId; } }
        public bool ShowDropDownUsers { get { return m_showDropDownUsers; } }

        public SearchEmployeesStoreData(int dropDownBudgetTypeIndex, int dropDownDepIndex, int dropDownSemesterIndex, 
            int dropDownUsersIndex, string textBoxSearchId, bool showDropDownUsers)
        {
            m_dropDownBudgetTypeIndex = dropDownBudgetTypeIndex;
            m_dropDownDepIndex = dropDownDepIndex;
            m_dropDownSemesterIndex = dropDownSemesterIndex;
            m_dropDownUsersIndex = dropDownUsersIndex;
            m_textBoxSearchId = textBoxSearchId;
            m_showDropDownUsers = showDropDownUsers;
        }
    }
}
